<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\VersionOp;
use App\Http\Controllers\Controller;
use App\Model\HostConfigInfo;
use App\Util\AliyunUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class HostConfigInfoController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('声优资费和星标管理');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * 编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(HostConfigInfo::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->charm_range('声优魅力值')->display(function ($data) {
                return $this->charm_min . '~' . $this->charm_max;
            });
            $grid->coin_setting('可选资费')->display(function ($data) {
                return "{$data}金币/分钟";
            });
            $grid->star_icon('星标')->display(function ($data) {
                return !$data ? '-' : "<img src='" . AliyunUtil::IMG_HOST . $data . "' width='80' height='80'/>";
            });

            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                // $actions->disableEdit();
                $actions->disableView();
            });
        });
    }

    /**
     * 新建
     * @return Form
     */
    protected function form()
    {
        return Admin::form(HostConfigInfo::class, function (Form $form) {
            // $form->display('id', '用户ID');
            $form->text('charm_min', '魅力值范围（小）')->default('');
            $form->text('charm_max', '魅力值范围（大）')->default('');
            $form->select('coin_setting', '可选资费')->default(0)->options(HostConfigInfo::getCoinSetting());
            $form->image('star_icon', '星标')->uniqueName();
        });
    }
}
